﻿using aier.dataaccess.model;
using aier.dataaccess.dapper;
using aier.xrm.wcf;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.Text.RegularExpressions;

namespace aier.business.logic
{
    public class CommonUtil
    {
        /// <summary>
        /// 移除不用保留实体属性
        /// </summary>
        /// <param name="targetEntity">目标实体</param>
        /// <param name="attrNames">带保留属性</param>
        public static void RemoveEntityAttributes(Entity targetEntity, string[] attrNames)
        {
            List<string> deleteAttrList = new List<string>();
            foreach (var attrName in targetEntity.Attributes.Keys)
            {
                var retainAttr = attrNames.Where(n => n == attrName).FirstOrDefault();
                if (string.IsNullOrWhiteSpace(retainAttr))
                    deleteAttrList.Add(attrName);
            }
            if (deleteAttrList.Count() > 0)
            {
                foreach (var deleteAttr in deleteAttrList)
                {
                    targetEntity.Attributes.Remove(deleteAttr);
                }
            }
        }

        /// <summary>
        /// 移除实体不必要属性值
        /// </summary>
        /// <param name="targetEntity">目标实体</param>
        /// <param name="attrNames">带移除的属性</param>
        public static void RetainEntityAttributes(Entity targetEntity, string[] attrNames)
        {
            List<string> deleteAttrList = new List<string>();
            foreach (var attrName in targetEntity.Attributes.Keys)
            {
                var retainAttr = attrNames.Where(n => n == attrName).FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(retainAttr))
                    deleteAttrList.Add(attrName);
            }
            if (deleteAttrList.Count() > 0)
            {
                foreach (var deleteAttr in deleteAttrList)
                {
                    targetEntity.Attributes.Remove(deleteAttr);
                }
            }
        }

        /// <summary>
        /// 检查dynamics 是否为空
        /// </summary>
        /// <param name="dyparam"></param>
        /// <returns></returns>
        public static bool isDynamicNull(dynamic dyparam)
        {
            return Object.ReferenceEquals(null, dyparam);
        }
        /// <summary>
        /// 验证是否是手机号码
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static bool IsMobile(string condition)
        {
            if (condition.Length != 11)
            {
                return false;
            }
            return Regex.IsMatch(condition, @"^1(3|4|5|6|7|8|9)\d{9}");
        }

        /// <summary>
        /// 判断是否为汉字或字母
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static bool IsContainChinese(string condition)
        {
            if (Regex.IsMatch(condition.ToString(), @"[\u4e00-\u9fbb]+") ||
                Regex.IsMatch(condition.ToString(), @"[a-zA-Z]"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否为身份证
        /// </summary>
        /// <param name="citizenid">身份证号</param>
        /// <returns></returns>
        public static bool IsIDCard(string citizenid)
        {
            return Regex.IsMatch(citizenid, @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", RegexOptions.IgnoreCase);
        }
    }
}
